package controllers

import (
	"github.com/astaxie/beego"
	m"xmy/models/admin"
	"fmt"

	"strconv"
	"reflect"
)

type ActivityController struct {
	beego.Controller
}

//返回值 封装为 JSON 格式
func (this *ActivityController) Rsp(status bool, str string) {
	this.Data["json"] = &map[string]interface{}{"status": status, "info": str}
	this.ServeJSON()
}
//活动管理面板
func (c *ActivityController)  ActivityManage() {
	fmt.Println("进来--活动管理--控制器啦！")
	c.TplName = "marketing/activityManage.html"
}


//活动管理列表
func (this *ActivityController)  ActivityManagePage() {
	fmt.Println("进来--活动列表--控制器啦！")
	page, _ := this.GetInt64("page")
	fmt.Println("page:", page)
	page_size, _ := this.GetInt64("rows")
	fmt.Println("page_size:", page_size)
	sort := this.GetString("sort")
	fmt.Println("sort:", sort)
	order := this.GetString("order")
	fmt.Println("order:", order)
	if len(order) > 0 {
		if order == "desc" {
			sort = "-" + sort
		}
	} else {
		sort = "Id"
	}
	nodes, count := m.GetActivitylist(page, page_size, sort)
	this.Data["json"] = &map[string]interface{}{"total": count, "rows": &nodes}
	fmt.Println(this.Data["json"])
	this.ServeJSON()
	return

}
//添加新闻
func (c *ActivityController)  AddActivity() {
	fmt.Println("进来--添加活动--控制器啦！")
	g := m.Activity{}
	if err := c.ParseForm(&g); err != nil {
		//handle error
		c.Rsp(false, err.Error())
		return
	}
	id, err := m.AddActivity(&g)
	fmt.Println("-----id:", id)
	if err == nil && id > 0 {
		c.Rsp(true, "Success")
		return
	} else {
		c.Rsp(false, err.Error())

		return
	}

}
//加入回收站
func (c *ActivityController)  RecycleActivity() {
	fmt.Println("进来--加入回收站--控制器啦！")
	Id := c.GetString("Id")
	Activity_id, err := strconv.ParseInt(Id, 10, 64)
	//打印参数类型
	fmt.Println("type:", reflect.TypeOf(Activity_id))
	id, err := m.Recycle_Activity_One(Activity_id)
	if err == nil && id > 0 {
		c.Rsp(true, "1")
		return
	} else {
		c.Rsp(true, "0")
		return
	}
}

//回收站管理
func (c *ActivityController)  ActivityManageRecycle() {
	fmt.Println("进来--活动回收站管理--控制器啦！")
	c.TplName = "marketing/activityManageRecycle.html"
}

//回收站管理列表
func (this *ActivityController)  ActivityManageRecyclePage() {
	fmt.Println("进来--活动回收站列表--控制器啦！")
	page, _ := this.GetInt64("page")
	fmt.Println("page:", page)
	page_size, _ := this.GetInt64("rows")
	fmt.Println("page_size:", page_size)
	sort := this.GetString("sort")
	fmt.Println("sort:", sort)
	order := this.GetString("order")
	fmt.Println("order:", order)
	if len(order) > 0 {
		if order == "desc" {
			sort = "-" + sort
		}
	} else {
		sort = "Id"
	}
	nodes, count := m.GetActivityRecyclelist(page, page_size, sort)
	this.Data["json"] = &map[string]interface{}{"total": count, "rows": &nodes}
	fmt.Println(this.Data["json"])
	this.ServeJSON()
	return

}

//从回收站还原活动
func (c *ActivityController)  ReductionActivity() {
	fmt.Println("进来--还原活动--控制器啦！")
	Id := c.GetString("Id")
	Activity_id, err := strconv.ParseInt(Id, 10, 64)
	//打印参数类型
	fmt.Println("type:", reflect.TypeOf(Activity_id))
	id, err := m.Reduction_Activity_One(Activity_id)
	if err == nil && id > 0 {
		c.Rsp(true, "1")
		return
	} else {
		c.Rsp(true, "0")
		return
	}
}

//从回收站删除活动
func (c *ActivityController)  DeletActivity() {
	fmt.Println("进来--删除活动--控制器啦！")
	Id := c.GetString("Id")
	Activity_id, err := strconv.ParseInt(Id, 10, 64)
	//打印参数类型
	fmt.Println("type:", reflect.TypeOf(Activity_id))
	id, err := m.Delete_Activity_One(Activity_id)
	if err == nil && id > 0 {
		c.Rsp(true, "1")
		return
	} else {
		c.Rsp(true, "0")
		return
	}
}

